$(function () {
    findById();
    getRoleByUserId();
});
function findById() {
    let id = sessionStorage.getItem("userId");
    let result = myAjax("/api/rb/user/findById", {id: id}, "get");
    if (result != null) {
        setData(result.data);
    }
}
function setData(data) {
    $("#nickName").val(data.nickName);
}
/**
 * 根据用户id拿到其拥有的权限信息和未拥有的权限
 */
function getRoleByUserId() {
    let id = sessionStorage.getItem("userId");
    let is = myAjax("/api/rb/user/getIsRoleByUserId", {id: id}, "post");
    let no = myAjax("/api/rb/user/getNoRoleByUserId", {id: id}, "post");
    if (is.code === 200 && no.code === 200) {
        setRoleData(is.data, no.data);
    }
    if(is.code !== 200 && no.code === 200){
        setRoleData([], no.data);
    }
    if(is.code === 200 && no.code !== 200){
        setRoleData(is.data, []);
    }
    if(is.code !== 200 && no.code !== 200){
        layer.alert(is.message, {icon: 2});
    }
}

function setRoleData(is, no) {

    let data1 = [];
    for (let i = 0; i < no.length; i++) {
        data1.push({
            value: no[i].id,
            title: no[i].name
        })
    }
    let data2 = [];
    for (let i = 0; i < is.length; i++) {
        data1.push({
            value: is[i].id,
            title: is[i].name
        });
        data2.push(is[i].id);
    }


    // console.log(data1,"hello");
    // console.log(data2,">>>>>>>>");

    layui.use(['transfer', 'layer', 'util'], function () {
        var $ = layui.$
            , transfer = layui.transfer;

        //定义标题及数据源
        transfer.render({
            elem: '#test2'
            , title: ['未拥有角色', '拥有角色']  //自定义标题
            , data: data1
            //,width: 150 //定义宽度
            , height: 400 //定义高度
            , parseData: function (res) {//数据参数来源data1转换处理
                return {
                    "value": res.value //data1属性
                    , "title": res.title//data1属性
                    , "roleId": res.value//data1属性
                    , "userId": sessionStorage.getItem("userId")
                }
            }
            , showSearch: true//显示搜索框
            , value: data2//用于回显或默认选中
            , onchange: function (data, index) {
                if (index === 0) {
                    // console.log(data, ">>>>>>>>>>>>>>"); //得到当前被穿梭的数据
                    // console.log(index); //如果数据来自左边，index 为 0，否则为 1
                    let result = teamAjax("/api/rb/urr/adds", JSON.stringify(data), "post");
                    if (result.code === 200) {
                        layer.alert(result.message, {icon: 1});
                    } else {
                        layer.alert(result.message, {icon: 2});
                    }
                } else {
                    // console.log(data, "<<<<<<<<<<<<<<<"); //得到当前被穿梭的数据
                    let result = teamAjax("/api/rb/urr/dels", JSON.stringify(data), "delete");
                    if (result.code === 200) {
                        layer.alert(result.message, {icon: 1});
                    } else {
                        layer.alert(result.message, {icon: 2});
                    }
                }
            }
        });

    });

}
// function save() {
//     let data = {
//         userId:sessionStorage.getItem("userId"),
//         roleId:$("#role").val()
//     };
//     let result=myAjax("/api/rb/urr/add",data,"post");
//     if(result.code===200){
//         alert(result.message);
//         x_admin_close();
//         x_admin_father_reload();
//     }else{
//         alert(result.message);
//     }
// }